<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('风险管理') }}</span>
    </div>
    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealRiskList" border>
      <el-table-column :label="$t('风险名称')" align="center" prop="riskName" show-overflow-tooltip />
      <el-table-column :label="$t('风险等级')" align="center" prop="riskLevel" :formatter="levelFormat" />
      <el-table-column :label="$t('风险类型')" align="center" prop="riskType" :formatter="riskTypeFormat" />
      <el-table-column :label="$t('发生概率')" align="center" prop="probability" :formatter="probabilityFormat" />
      <el-table-column :label="$t('损失程度')" align="center" prop="loss" :formatter="lossFormat" />
      <el-table-column :label="$t('风险状态')" align="center" prop="riskState" :formatter="riskStateFormat" />
      <el-table-column :label="$t('时间')" align="center" prop="riskTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.riskTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="isShow" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:risk:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:risk:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风险管理对话框 -->
    <el-dialog :title="$t('风险管理')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealRiskForm ref="dealRiskForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealRisk, delDealRisk } from '@/api/deal/risk/dealRisk'
import dealRiskForm from '@/views/deal/risk/dealRiskForm.vue'
export default {
  components: {
      dealRiskForm
  },
  data() {
      return {
        // 遮罩层
        loading: true,
        isShow: true,
        // 风险管理表格数据
        dealRiskList: [],
        // 是否显示弹出层
        formOpen: false,
        // 风险等级、发生概率、损失程度 字典level字典
        levelOptions: [],
        // 风险类型 字典risk_type字典
        riskTypeOptions: [],
        // 风险状态 字典risk_status字典
        riskStateOptions: [],
        // 查询参数
        queryParams: {
            dealId: this.$route.query.id,
            pageNum: 1,
            pageSize: 5
        },
        // 表单参数
        form: {},
        // 总条数
        total: 0,
        // 表单校验
        rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('level').then(response => {
          this.levelOptions = response.data
      })
      this.getDicts('risk_type').then(response => {
          this.riskTypeOptions = response.data
      })
      this.getDicts('risk_status').then(response => {
          this.riskStateOptions = response.data
      })
  },
  methods: {
    /** 查询风险管理列表 */
    getList() {
        this.loading = true
        listDealRisk(this.queryParams).then(response => {
            this.dealRiskList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 风险等级 字典level字典翻译
    levelFormat(row) {
      return this.selectDictLabel(this.levelOptions, row.riskLevel)
    },
    probabilityFormat(row) {
      return this.selectDictLabel(this.levelOptions, row.probability)
    },
    lossFormat(row) {
      return this.selectDictLabel(this.levelOptions, row.loss)
    },
    // 风险状态 字典risk_status字典翻译
    riskStateFormat(row) {
      return this.selectDictLabel(this.riskStateOptions, row.riskState)
    },
    // 风险类型
    riskTypeFormat(row) {
      return this.selectDictLabel(this.riskTypeOptions, row.riskType)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            riskName: undefined,
            riskLevel: undefined,
            riskType: undefined,
            probability: undefined,
            loss: undefined,
            riskState: undefined,
            riskTime: undefined,
            remark: undefined,
            closeCause: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealRiskForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealRisk(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    }
  }
}
</script>
